﻿using System;
using System.Collections;
using System.Collections.Generic;

namespace Snmp.Agent.MplsLsrMib
{
    class IdStringComparer : IComparer
    {
        public int Compare(object x, object y)
        {
            string[] xEls = ((string)x).Split(new[] { '.' }, StringSplitOptions.RemoveEmptyEntries);
            string[] yEls = ((string)y).Split(new[] { '.' }, StringSplitOptions.RemoveEmptyEntries);

            int elsCount = xEls.Length < yEls.Length ? xEls.Length : yEls.Length;

            int comp = 0;

            if (xEls.Length > yEls.Length)
            {
                comp = 1;
            }
            else if (xEls.Length < yEls.Length)
            {
                comp = -1;
            }

            for (int i = 0; i < elsCount; i++)
            {
                if (xEls[i].Length > yEls[i].Length)
                {
                    comp = 1;
                    break;
                }
                if (xEls[i].Length < yEls[i].Length)
                {
                    comp = -1;
                    break;
                }
                if (xEls[i].CompareTo(yEls[i]) == 1)
                {
                    comp = 1;
                    break;
                }
                if (xEls[i].CompareTo(yEls[i]) == -1)
                {
                    comp = -1;
                    break;
                }
            }

            return comp;
        }
    }
}
